import { instantiate, Prefab, Node, director, find, Vec3 } from "cc";
import { ResourceUtil } from "../AshFramework/Utils/ResourceUtil";
import { HintController } from "./GameUI/HintController";
import { AwardController } from "./GameUI/AwardController";
import { awardMasonry, AWARYTYPE, EPOCH, LV, Task, TASKLABEL, taskRequire, TASKTYPE } from "../AshFramework/Datas/GameConstant";
import { ZTool } from "../AshFramework/Utils/ZTool";
import { GameManager } from "./GameManager";

const taskTypeValues = [
    TASKTYPE.GOLD,
    TASKTYPE.MONSTER,
    TASKTYPE.FOOD,
    TASKTYPE.CONSUME,
];

export class GameUtil {

    public static formatNumber(num: number): string {
        if (Math.abs(num) >= 1e15) {
            return `999T`;
        } else if (Math.abs(num) >= 1e12) {
            return `${(num / 1e12).toPrecision(3)}T`;
        } else if (Math.abs(num) >= 1e9) {
            return `${(num / 1e9).toPrecision(3)}B`;
        } else if (Math.abs(num) >= 1e6) {
            return `${(num / 1e6).toPrecision(3)}M`;
        } else if (Math.abs(num) >= 1e3) {
            return `${(num / 1e3).toPrecision(3)}K`;
        } else if (Math.abs(num) >= 1e2) {
            return num.toPrecision(3);
        } else if (Math.abs(num) >= 1e1) {
            return num.toPrecision(2);
        } else {
            return num.toPrecision(1);
        }
    }

    public static getBetween(num: number, min: number, max: number): number {
        if (num >= max) {
            return max;
        } else if (num <= min) {
            return min;
        } else {
            return num;
        }
    }

    /** 
     * 获取时间戳到现在时间的秒
    */
    public static getSecondByTimestamp(preTimestamp) {
        let nowDate = new Date();
        return Math.floor((nowDate.getTime() - preTimestamp) / 1000);
    }

    public static showHint(test: string) {
        ResourceUtil.LoadPrefab("Game/Hint").then((prefab: Prefab) => {
            let hint: Node = instantiate(prefab);
            find("Canvas", director.getScene()).addChild(hint);
            hint.getComponent(HintController).showHint(test);
        })
    }
    //**玩家看视频 游戏结束之后的奖励动画 */
    public static getAward(type: AWARYTYPE, targetPos: Vec3, cb: Function = null) {
        ResourceUtil.LoadPrefab("Game/Award").then((prefab: Prefab) => {
            let Award: Node = instantiate(prefab);
            find("Canvas", director.getScene()).addChild(Award);
            Award.getComponent(AwardController).init(type, targetPos, cb);
        })
    }

    //**玩家击毙怪兽之后的奖励动画 */
    public static getAwardByMonster(startPos: Vec3, targetPos: Vec3, cb: Function = null) {
        ResourceUtil.LoadPrefab("Game/Award").then((prefab: Prefab) => {
            let Award: Node = instantiate(prefab);
            find("Canvas", director.getScene()).addChild(Award);
            Award.setWorldPosition(startPos);
            Award.getComponent(AwardController).initGold(targetPos, cb);
        })
    }

    public static getDistanceByNode(startNode: Node, endNode: Node) {
        return startNode.getWorldPosition().clone().subtract(endNode.getWorldPosition().clone()).length();
    }

    public static getTask(lv: LV, count: number) {
        let task: Task = {
            type: TASKTYPE.CONSUME,
            label: "",
            curProgress: 0,
            totalProgress: 0,
        } as Task;
        const randomIndex = Math.floor(Math.random() * taskTypeValues.length);
        // task.type = taskTypeValues[randomIndex];
        task.type = TASKTYPE.CONSUME;
        task.curProgress = 0;
        let num = taskRequire[task.type][lv];
        task.totalProgress = Math.floor(ZTool.GetRandomInt(num / 2, num));
        task.label = TASKLABEL[task.type];
        task.award = awardMasonry[count - 1];
        return task;
    }
}


